/**
 * @typedef CreateArticle
 * @property {string} articleTitle.required -文章标题
 * @property {string} content.required -文章内容
 * @property {string} html.required -文章html
 * @property {string} category.required -文章分类
 * @property {string} tag.required -文章标签
 * @property {string} coverImage -文章封面url
 * @property {string} description -文章描述
 * @property {string} linkUrl -文章来源
 */

/**
 * @typedef UpdateArticle
 * @property {string} id.required -文章标识
 * @property {string} articleTitle.required -文章标题
 * @property {string} content.required -文章内容
 * @property {string} html.required -文章html
 * @property {string} category.required -文章分类
 * @property {string} tag.required -文章标签
 * @property {string} coverImage -文章封面url
 * @property {string} description -文章描述
 * @property {string} linkUrl -文章来源
 */

/**
 * @typedef ExportArticle
 * @property {string} articleId.required -文章id
 */

/**
 * @typedef RecommendArticle
 * @property {string} articleId -文章id
 * @property {string} tags.required -文章标签
 */

/**
 * @typedef AuthorRecommendArticle
 * @property {string} articleId.required -文章id
 * @property {string} userId.required -用户id
 */

const express = require('express')
const router = express.Router()
const auth = require('@/middleware/auth.middleware')
const articleController = require('@/controllers/article.controller')
const handlerValidate = require('@/middleware/handlerValidate.middleware')
const { updateArticleShema } = require('@/middleware/validators/articleValidator.middleware')

/**
 * 审核通过的文章列表分页查询
 * @route GET /articles
 * @group 文章管理
 * @param {string} pageNo.query.required
 * @param {string} pageSize.query.required
 * @param {string} category.query
 * @param {string} sort.query
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 */
router.get('/', articleController.getAllArticles)

/**
 * 根据用户id查询用户下的文章分页列表
 * @route GET /articles/listByUserId
 * @group 文章管理
 * @param {string} pageNo.query.required
 * @param {string} pageSize.query.required
 * @param {string} userId.query
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 */
router.get('/listByUserId', articleController.getArticlesByUserId)

/**
 * 文章详情查询
 * @route GET /articles/articleinfo
 * @group 文章管理
 * @param {string} id.query.required
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 */
router.get('/articleinfo', articleController.getArticleInfo)

/**
 * 文章md查询
 * @route GET /articles/mdinfo
 * @group 文章管理
 * @param {string} id.query.required
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 * @security JWT
 */
router.get('/mdinfo', auth(), articleController.getArticleMarkdown)

/**
 * 创建文章
 * @route POST /articles/createarticle
 * @group 文章管理
 * @param {CreateArticle.model} createArticle.body.required
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 * @security JWT
 */
router.post('/createarticle', auth(), articleController.createArticle)

/**
 * 更新文章
 * @route POST /articles/updatearticle
 * @group 文章管理
 * @param {UpdateArticle.model} udateArticle.body.required
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 * @security JWT
 */
router.post('/updatearticle', auth(), updateArticleShema, handlerValidate(articleController.updateArticle))

/**
 * 删除文章
 * @route GET /articles/deletearticle
 * @group 文章管理
 * @param {string} id.query.required
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 * @security JWT
 */
router.get('/deletearticle', auth(), handlerValidate(articleController.deleteArticle))

/**
 * 查询被用户收藏的文章列表
 * @route GET /articles/collected
 * @group 文章管理
 * @param {string} userId.query.required
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 * @security JWT
 */
router.get('/collected', articleController.getArticleByCollect)

/**
 * 查询被用户点赞过的文章列表
 * @route GET /articles/agreed
 * @group 文章管理
 * @param {string} userId.query.required
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 * @security JWT
 */
router.get('/agreed', articleController.getArticleByAgree)

/**
 * 导出文章
 * @route POST /articles/export
 * @group 文章管理
 * @param {ExportArticle.model} exportArticle.body.required
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 * @security JWT
 */
router.post('/export', auth(), handlerValidate(articleController.exportArticle))

/**
 * 推荐文章查询
 * @route POST /articles/recommend
 * @group 文章管理
 * @param {RecommendArticle.model} recommendArticle.body.required
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 */
router.post('/recommend', articleController.findRecommendArticles)

/**
 * 用户推荐文章查询
 * @route POST /articles/author-recommend
 * @group 文章管理
 * @param {AuthorRecommendArticle.model} authorRecommendArticle.body.required
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 */
router.post('/author-recommend', articleController.getAuthorRecommendArticles)

/**
 * 置顶和取消置顶文章
 * @route GET /articles/operateTop
 * @group 文章管理
 * @param {string} articleId.query.required
 * @param {string} type.query.required
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 */
router.get('/operateTop', auth(), articleController.setArticleTop)

/**
 * 文章审核结果获取
 * @route POST /articles/get-approvel-result
 * @group 文章管理
 * @returns {object} 200
 * @returns {Error}  default - Unexpected error
 */
router.post('/get-approvel-result', articleController.approvelResult)

module.exports = router
